extends Label


class_name PropertyUiLabel
"""
监听并格式化显示的属性 "Exp:${value}/${max_value} Lv:${level}"
"""


@export var property_component: PropertyComponent = null


var __format_text: String = ""
var __property_list: Array = []


func __check_exports() -> Dictionary:
    return {
        "property_component": func (): return property_component != null
    }

func _get_configuration_warnings():
    return IComponent.editor_warnings(self)

func _ready() -> void:
    property_component.event_value_will_changed.connect(on_value_will_changed)

    __format_text = text
    __property_list = extract_bracketed_content(__format_text)
    update_value("", null)

func extract_bracketed_content(input: String) -> Array:
    var regex = RegEx.new()
    regex.compile("[{\\$](.*?)[}]")
    var matches = []
    for r in regex.search_all(input):
        var v = r.get_string()
        matches.append(v.substr(2, v.length() - 3))
    return matches

func update_value(v_name: String, value: Variant) -> void:
    var display_text = __format_text
    for p in __property_list:
        var v = value if v_name == p else property_component.get(p)
        if v != null:
            display_text = NodeHelper.replace_variable(display_text, {p: v})
    text = display_text

func on_value_will_changed(cmpt: PropertyComponent, v_name: String, current_v: Variant, value: Variant) -> void:
    if __property_list.has(v_name):
        update_value(v_name, value)
